﻿using AutoMapper;
using Inventory.API.Application.Command.StoragelocationmanagementCommand;
using Inventory.Domain;
using Inventory.Errorcode;
using Inventory.Errorcode.ErrorCode;
using Inventory.Infrastructure.Interfaces;
using MediatR;

namespace Inventory.API.Application.CommandHandler.StoragelocationmanagementCommandHandler
{
    public class AddStoragelocationmanagementCommandHanlder : IRequestHandler<AddStoragelocationmanagementCommand, ApiResult>
    {

        private readonly IStoragelocationmanagementRepository repository;
        private readonly IMapper mapper;

        private readonly ILogger<AddStoragelocationmanagementCommandHanlder> _logger;

        public AddStoragelocationmanagementCommandHanlder(IStoragelocationmanagementRepository repository, IMapper mapper, ILogger<AddStoragelocationmanagementCommandHanlder> logger)
        {
            this.repository = repository;       
            this.mapper = mapper;
            _logger = logger;
        }
        public async Task<ApiResult> Handle(AddStoragelocationmanagementCommand request, CancellationToken cancellationToken)
        {

            try
            {
                var storagelocation = mapper.Map<Storagelocationmanagement>(request);

                await repository.InsertAsync(storagelocation);

                return ApiResult.Success(StatusCodeEnum.Success);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message, "An error occurred while creating a new storage location.");
                throw;
            }
           
          
      
        }
    }
}
